//
//  DDOrderSectionHeader.m
//  Dingdong
//
//  Created by 程强 on 16/11/15.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDOrderSectionHeader.h"

typedef void(^DeleteOrderHandler)();

@interface DDOrderSectionHeader()
@property (strong, nonatomic) KKLabel *shopNameLbl;
@property (strong, nonatomic) KKLabel *orderStatusLbl;
@property (strong, nonatomic) UIButton *deleteBtn;
@property (strong, nonatomic) DeleteOrderHandler deleteBlock;
@end

@implementation DDOrderSectionHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.shopNameLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",RGBColor(51, 51, 51),[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [self addSubview:_shopNameLbl];
        [_shopNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).offset(15);
            make.width.equalTo(@180);
            make.height.equalTo(@30);
        }];
        
        self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setBackgroundImage:LoadImage(@"order_del") forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_deleteBtn];
        [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.width.equalTo(@17);
            make.height.equalTo(@18);
            make.right.equalTo(self).offset(-15);
        }];
        
        self.orderStatusLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",RGBColor(255, 80, 80),[UIFont systemFontOfSize:13.0],NSTextAlignmentRight);
        }];
        [self addSubview:_orderStatusLbl];
        [_orderStatusLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(_deleteBtn.mas_left).offset(-10);
            make.height.equalTo(@30);
            make.left.equalTo(_shopNameLbl.mas_right).offset(15);
        }];
        
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = RGBColor(237, 237, 237);
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.equalTo(@1);
        }];
        
        self.canDelete = NO;
    }
    return self;
}
- (void)setShopName:(NSString *)shopName
{
    _shopName = shopName;
    _shopNameLbl.text = shopName;
}
- (void)setOrderStatus:(kOrderStatus)orderStatus
{
    _orderStatus = orderStatus;
    NSString *status = @"";
    UIColor *textColor = RGBColor(255, 80, 80);
    switch (orderStatus) {
        case kOrderStatusPending:
            status = @"Unpaid";
            break;
        case kOrderStatusConfirm:
            status = @"Confirmed";
            break;
        case kOrderStatusShipped:
            status = @"Shipped";
            break;
        case kOrderStatusReceived:
            status = @"Received";
            break;
            textColor = RGBColor(150, 150, 150);
        case kOrderStatusComplete:
            status = @"Completed";
            textColor = RGBColor(150, 150, 150);
            break;
        case kOrderStatusCancel:
            status = @"Canceled";
            textColor = RGBColor(150, 150, 150);
            break;
        default:
            break;
    }
    _orderStatusLbl.text = status;
    _orderStatusLbl.textColor = textColor;
}
- (void)setCanDelete:(BOOL)canDelete
{
    if (canDelete) {
        _deleteBtn.hidden = NO;
        [_deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@17);
        }];
    }else{
        _deleteBtn.hidden = YES;
        [_deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@0);
        }];
    }
}
- (void)deleteClick:(UIButton *)sender
{
    if (self.deleteBlock) {
        self.deleteBlock();
    }
}
- (void)userDeleteOrderHandler:(void(^)())block
{
    self.deleteBlock = block;
}
@end
